﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace DATN.mds.Note
{
    public partial class EditNote : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                Load_data();
                //TextBox1.Enabled = false;
                TextBox2.Enabled = false;
                TextBox3.Enabled = false;
                TextBox4.Enabled = false;
                //TextBox5.Enabled = false;
                btt_save.Enabled = false;

            }
            Button1.Attributes.Add("onclick", "return confirm('Bạn có chắc chắn xóa không?');");

        }

        protected void Load_data()
        {
            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
            cnn.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("Select * From Notes Where No_Id = " + Convert.ToInt32(Session["No_Id"].ToString()), cnn);

            da.Fill(ds, "Notes");

            //TextBox1.Text = ds.Tables["Notes"].Rows[0].ItemArray[0].ToString();
            TextBox2.Text = ds.Tables["Notes"].Rows[0].ItemArray[1].ToString();
            TextBox3.Text = ds.Tables["Notes"].Rows[0].ItemArray[2].ToString();
            string time = ds.Tables["Notes"].Rows[0].ItemArray[3].ToString();
            //string date = ds.Tables["Notes"].Rows[0].ItemArray[3].ToString();

            //DateTime date1 = Convert.ToDateTime(date);
            DateTime time1 = Convert.ToDateTime(time);

            TextBox4.Text = time1.ToString("dd/MM/yyyy hh:mm:ss");
            
            //TextBox5.Text = date1.ToString("dd/MM/yyyy");

            ds.Dispose();
            da.Dispose();
            cnn.Close();
        }
        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        protected void btt_edit_Click(object sender, EventArgs e)
        {
            TextBox2.Enabled = true;
            TextBox3.Enabled = true;
            TextBox4.Enabled = true;
            //TextBox5.Enabled = true;
            btt_save.Enabled = true;
        }

        protected void btt_save_Click(object sender, EventArgs e)
        {
            if(IsPostBack)
            {
                string name = TextBox2.Text;
                string descrip = TextBox3.Text;
                string timeremind = TextBox4.Text;
                string DateCreate = timeremind;
                DateTime date1 = new DateTime();
                try
                {
                    date1 = Convert.ToDateTime(DateCreate);
          
                DateCreate = date1.ToString("MM/dd/yyyy hh:mm:ss"); 
                     
                //string timeremind = time.ToShortTimeString();
                //string DateCreate = Date.ToShortDateString();
                SqlConnection cnn = new SqlConnection();
                cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                //string com = "Insert into Customers(Name, Birthday, Sex, Phone, Mail, Address, HomeTown, Company, MarritalStatus, Job, Salary, Habits, DateCreate) values "+
                //     ""+ "('NHA', '12-12-1999', 'Nam', '0919138556', 'nv.hien89@yahoo.com', 'Doi 3, MinhKhai', 'Ha Noi', 'FPT', 'Đã Cưới', 'Kỹ Sư','2,222','Go out and say','11-11-2012')";

                string com1 = "UpDate Notes "
                    + "\n Set Name = N'" + name + "',"
                    + "Description = N'" + descrip + "',"
                    + "TimeRemind = '" + DateCreate + "'"
                    + "\n Where No_Id =" + Convert.ToInt32(Session["No_Id"].ToString());

                SqlCommand cmd = cnn.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = com1;

                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();

                Page.Response.Redirect("~/mds.Note/IndexNote.aspx");
                }
                catch (Exception ex)
                {
                    Label1.Text = "Xin Nhập Lại Ngày Và Giờ";
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();

            string com = "Delete From Notes Where No_Id = " + Convert.ToInt32(Session["No_Id"].ToString());

            SqlCommand cmd = cnn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = com;

            cnn.Open();
            cmd.ExecuteNonQuery();
            cnn.Close();

            Page.Response.Redirect("~/mds.Note/IndexNote.aspx");

        }
    }
}